package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ResourceDao;
import cn.edu.sdjzu.xg.bysj.dao.RoleAssResourceDao;
import cn.edu.sdjzu.xg.bysj.dao.RoleDao;
import cn.edu.sdjzu.xg.bysj.domain.authority.Resource;
import cn.edu.sdjzu.xg.bysj.domain.authority.Role;
import cn.edu.sdjzu.xg.bysj.domain.authority.RoleAssResource;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

public final class ResourceService {
	//获取ResourceDao实例
	private static final ResourceDao resourceDao = ResourceDao.getInstance();
	//新增ResourceService对象
	private static final ResourceService resourceService = new ResourceService();
	//获取ResourceService对象
	public static ResourceService getInstance() {
		return resourceService;
	}
	/**
	 * 向ResourceDao发消息查询
	 * @param roleId
	 * @return
	 * @throws SQLException
	 */
	public Collection<Resource> findAll(Integer roleId) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Collection<RoleAssResource> roleAssResources =null;
		Collection<Resource> resources =null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			roleAssResources = RoleAssResourceDao.getInstance().findAll(connection, roleId);
			resources = new TreeSet<Resource>();
			for (RoleAssResource roleAssResource : roleAssResources){
				resources.add(roleAssResource.getResource());
			}
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return resources;
	}
	/**
	 * 向ResourceDao发消息根据页码查询
	 * @param pagination
	 * @param conditionList
	 *
	 * @return
	 * @throws SQLException
	 */
	public Collection<Resource> findAll(Pagination pagination, List<Condition> conditionList) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Collection<Resource> resources = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			resources = resourceDao.findAll(connection, pagination, conditionList);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return resources;
	}
	/**
	 * 向ResourceDao发消息根据id查找
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Resource find(Integer id) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Resource resource = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			resource = resourceDao.find(connection, id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return resource;
	}
	/**
	 * 向ResourceDao发消息更新
	 * @param resource
	 * @return
	 * @throws SQLException
	 */
	public boolean update(Resource resource) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean updated = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			updated = resourceDao.update(connection, resource);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return updated;
	}
	/**
	 * 向ResourceDao发消息新增
	 * @param resource
	 * @return
	 * @throws SQLException
	 */
	public boolean add(Resource resource) throws SQLException {
		Connection connection = JdbcHelper.getConn();
		boolean added = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			added = resourceDao.add(connection, resource);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return added;
	}
	/**
	 * 向ResourceDao发消息删除
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public boolean delete(Integer id) throws SQLException, BysjException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean deleted = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			deleted = resourceDao.delete(connection, id);
		} catch (SQLException e) {
			throw new BysjException("某角色正在使用当前资源");
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return deleted;
	}
}
